﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Filter;
using Cofe.Core.Implements;
using Cofe.Data.Utils.ThirdParty;

namespace Cofe.Data.Filter
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEDB)]
    public class FileNameEntryFilterExpression : 
        IEntryFilterExpression<FileNameEntryFilter>
    {
        public Expression<Func<CofeSystemInfo, bool>>
            GetQueryExpression(FileNameEntryFilter entryFilter, IOptionFilter[] options)
        {            
            string containStr = entryFilter.Value
                .Replace(".*", "")
                .Replace("*", "").Replace("?", "");

            var propExpr = CofeServices.PropertyDictionary.GetPropertyExpression<string>(CofeProperties.Name);
            Expression<Func<CofeSystemInfo, bool>> exp;

            if (String.IsNullOrEmpty(containStr))
                exp = e => true;
            else exp = e => e.DatabaseEntry.Label.Contains(containStr);
                               
      
            if (entryFilter.Negative)
                return exp.Not();
            return exp;
        }
    }
}
